package com.hmo.appshop.mamager.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import java.io.File;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * 控制台输出 Swagger 接口文档地址
 *
 * @author cyndi905
 * @Date 2020-10-6
 **/
@Component
@Slf4j
public class ServerConfig implements ApplicationListener<WebServerInitializedEvent> {

    @Value("${apk}")
    private String apk;
    @Value("${image}")
    private String image;

    @Override
    public void onApplicationEvent(WebServerInitializedEvent event) {
        try {
            InetAddress inetAddress = Inet4Address.getLocalHost();
            int serverPort = event.getWebServer().getPort();
            log.info("项目启动启动成功！接口文档地址: http://" + inetAddress.getHostAddress() + ":" + serverPort + "/swagger-ui.html");
            File file = new File(apk);
            File file1 = new File(image);
            if (!file.exists()&&!file.isDirectory()){
                file.mkdirs();
            }
            if (!file1.exists()&&!file1.isDirectory()){
                file1.mkdirs();
            }
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }

}

